/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.io.File;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.FileInputStream;
import  java.io.DataInputStream;
import  java.io.BufferedInputStream;
import  java.io.InputStreamReader;
import  java.io.BufferedReader;
import  java.io.IOException;

import  java.util.Iterator;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  com.evolve.webbot.bean.Coppel;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.evolve.webbot.file.CommonDataFile;
import java.util.Date;

/**
 *
 * @author DarthAE
 */
public class XlsFileCoppel
{
  private String filePath;

  
  public XlsFileCoppel(){}
  
  public XlsFileCoppel(String filePath)
  {
      this.filePath = filePath;
  }
  
  public ArrayList<String> getValuesFromXlsFile()
  {
     ArrayList<String> values = new ArrayList<String>();
     double numValue = 0;
     String value    = null;
     String strLine  = null;
     int k=0;
     
  
     try{
          FileInputStream fstream = new FileInputStream(this.filePath);
  
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader  br = new BufferedReader(new InputStreamReader(in));
      
  
          while ((strLine = br.readLine()) != null)   
          {
             for(int i=0;i<127;i++)
             {   
                 if( /*(i!=46 && i!=47) &&*/ (i<=44 || i>=58))
                 {
                    char n = (char)i;
                    strLine=strLine.replace(n,' ');
                 }                 
             }
             
             StringTokenizer tokens = new StringTokenizer(strLine);

             while(tokens.hasMoreTokens())
             {
                 value=tokens.nextToken().trim();

//                 try{
//                     numValue = Double.parseDouble(value);
                     if(value.compareTo("-")!=0 &&
                        value.compareTo("/")!=0 &&
                        value.compareTo(".")!=0 &&
                        value.compareTo(",")!=0)
                     {
                         values.add(value);
                     }
                     
//                    }catch(Exception e){System.out.println(e);}
                 
                 k++;
             }            
             k=0;
          }
          System.out.println("[tamaño de la coleccion] "+values.size());
          /*System.out.println(values.toString());*/
          in.close();
        }catch (Exception e){System.err.println("Error: " + e.getMessage());}

     return values;
  }

  public ArrayList<String> getValuesFromXlsFile(int opt)
  {
     ArrayList<String> values = new ArrayList<String>();
     /*double numValue = 0;*/
     String value    = null;
     String strLine  = null;
     int k=0;
     
  
     try{
          FileInputStream fstream = new FileInputStream(this.filePath);
  
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader  br = new BufferedReader(new InputStreamReader(in));
      
  
          while ((strLine = br.readLine()) != null)   
          {
             System.out.println(strLine);
          }
          System.out.println("[tamaño de la coleccion] "+values.size());
          /*System.out.println(values.toString());*/
          in.close();
        }catch (Exception e){System.err.println("Error: " + e.getMessage());}

     return values;
  }  
  
  public ArrayList<ArrayList<String>> getCoppelBeanValues(ArrayList<String> values)
  {
      ArrayList<String> doubleValues = new ArrayList<String>();
      ArrayList<ArrayList<String>> listBeanValues = new ArrayList<ArrayList<String>>();
      boolean inicio= false;
      String  linea = "";

      int     k     = 0;
      int     l     = 0;
      int     i     = 0;



      for(i=0;i<values.size();i++)
      {

          if(k>0)
          {
            //if(l<2)
            //{
                //if(Double.parseDouble(values.get(i))>0)
                //{
                //    doubleValues.add(Double.parseDouble(values.get(i)));
                //}
            //    l=0;
            //}
            //else
            //{
                //doubleValues.add(Double.parseDouble(values.get(i)));
                doubleValues.add(values.get(i));
            //}
            //l++;
          }

          if(values.get(i).compareTo("38008")==0)
          {

             if(!doubleValues.isEmpty())
             {
                listBeanValues.add(doubleValues);
             }
             
             doubleValues = new ArrayList<String>();
            
            
            k++;
          }


        /*
        if(values.get(i).compareTo("38008")!=0 &&
           inicio==true)
        {
            linea = linea + values.get(i)+" ";
        }
        else
            if(values.get(i).compareTo("38008")==0)
        {
            inicio = true;
            System.out.print(values.get(i)+" "+linea+"\n");
            linea="";
        }*/
      }
      doubleValues.add("38008");
      listBeanValues.add(doubleValues);
      //System.out.println("[tamaño de la coleccion de colecciones] "+listDoubleValues.size());
      /*System.out.println(listDoubleValues);*/

      for(int j=0;j<listBeanValues.size();j++)
      {
          int cs =listBeanValues.get(j).size();

          if(listBeanValues.get(j).size()==10 ||
             listBeanValues.get(j).size()> 12)
          {              
              fixTenValuesList(listBeanValues.get(j));
          }
          
          if(listBeanValues.get(j).size()==12)
          {
              listBeanValues.get(j).remove((listBeanValues.get(j).size() - 1));
          }   
          
          if(listBeanValues.get(j).size()!=11)
          {
              System.out.println("[tamaño coleccion]"+cs+" : "+listBeanValues.get(j));
          }
      }
      
      return listBeanValues;
  }
  
  public ArrayList<String> fixTenValuesList(ArrayList<String> orgValueList)
  {
      int i = 0;
      ArrayList<String> valueList = new ArrayList<String>();
      String trashValue = null;
      
      if(orgValueList.size()==10)
      {
          orgValueList.add(1,"0");
      }
      else
      if(orgValueList.size()>12)
      {
          trashValue = orgValueList.get(0);
          orgValueList.remove(0);
          
          while(i<orgValueList.size())
          {
              if(orgValueList.get(i).compareTo(trashValue)==0)
              {
                  orgValueList.remove(i);
              }
              
              i++;
          }
      }
      
      valueList.addAll(orgValueList);
      
      return valueList;
  }
  
  public void writeCoppelDataFile(ArrayList<ArrayList<String>> valueList,Date date)
  {
      ArrayList<Coppel> coppelList = new ArrayList<Coppel>();
      
      for(int i=0;i<valueList.size();i++)
      {
          Coppel coppel = new Coppel();
                  
          for(int j=0;j<valueList.get(i).size();j++)
          {
              coppel.setNomProveedor("MEGA BLOKS LATINOAMERICA");
              
              switch(j)
              {
                  case 0: coppel.setCodigo(valueList.get(i).get(j));
                          break;
                  case 1: coppel.setModelo(valueList.get(i).get(j));
                          break;
                  case 2: coppel.setPrecioCosto(valueList.get(i).get(j));
                          break;
                  case 3: coppel.setPrecioVenta(valueList.get(i).get(j));
                          break;
                  case 4: coppel.setVtaDelPer01(valueList.get(i).get(j));
                          break;
                  case 5: coppel.setVtaDelPer02(valueList.get(i).get(j));
                          break;
                  case 6: coppel.setVtaDelPer03(valueList.get(i).get(j));
                          break;
                  case 7: coppel.setVtaUltNPer(valueList.get(i).get(j));
                          break;
                  case 8: coppel.setExistencia(valueList.get(i).get(j));
                          break;
                  case 9: coppel.setRotacionSku(valueList.get(i).get(j));
                          break;
                  case 10:coppel.setNumProveedor(valueList.get(i).get(j));
                          break;
                  default:System.out.println("null value");
                          break;
              }
          }
          
          coppelList.add(coppel);
      }  
      
      CommonDataFile commonDataFile = new CommonDataFile();
      commonDataFile.writeCommonWebTxtFileCoppel(this.filePath,date,coppelList);
  }
  
  public static void main(String args[])
  {
      ArrayList<String> lista = new ArrayList<String>();
      XlsFileCoppel xlsFile = new XlsFileCoppel("C:\\meg4777_338305571_F79DB4B4XAB05X464DXBD35XA79EFC30903B.xls");
      lista = xlsFile.getValuesFromXlsFile(0);
      xlsFile.getCoppelBeanValues(lista);
  }
}
